﻿/*
 * @author: wizardc
 */

using System.IO;
using UnityEngine;

namespace Dou.Utils
{
    /// <summary>
    /// 流工具类
    /// </summary>
    public static class StreamUtil
    {
        private static bool IsAndroidStreamingAssets(string fullPath)
        {
            return fullPath.StartsWith("jar:file");
        }
        
        private static string GetRelativeStreamingAssetsPath(string fullPath)
        {
            var start = Application.streamingAssetsPath.Length;
            var path = Path.Combine("assets", fullPath.Substring(start));
            return path;
        }
        
        /// <summary>
        /// 获取指定文件的流
        /// </summary>
        public static Stream GetDataStream(string path)
        {
            var stream = default(Stream);
            var isAndroidStreamingData = IsAndroidStreamingAssets(path);
            if (isAndroidStreamingData)
            {
                var relative = GetRelativeStreamingAssetsPath(path);
                stream = BetterStreamingAssets.OpenRead(relative);
            }
            else
            {
                stream = File.OpenRead(path);
            }
            return stream;
        }
    }
}
